home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / corelib / ncbisgml.c < prev    next >
Text File  |  1996-07-05  |  7KB  |  259 lines

  1. /*   ncbimisc.c
  2. * ===========================================================================
  3. *
  4. *                            PUBLIC DOMAIN NOTICE                          
  5. *               National Center for Biotechnology Information
  6. *                                                                          
  7. *  This software/database is a "United States Government Work" under the   
  8. *  terms of the United States Copyright Act.  It was written as part of    
  9. *  the author's official duties as a United States Government employee and 
  10. *  thus cannot be copyrighted.  This software/database is freely available 
  11. *  to the public for use. The National Library of Medicine and the U.S.    
  12. *  Government have not placed any restriction on its use or reproduction.  
  13. *                                                                          
  14. *  Although all reasonable efforts have been taken to ensure the accuracy  
  15. *  and reliability of the software and data, the NLM and the U.S.          
  16. *  Government do not and cannot warrant the performance or results that    
  17. *  may be obtained by using this software or data. The NLM and the U.S.    
  18. *  Government disclaim all warranties, express or implied, including       
  19. *  warranties of performance, merchantability or fitness for any particular
  20. *  purpose.                                                                
  21. *                                                                          
  22. *  Please cite the author in any work or product based on this material.   
  23. *
  24. * ===========================================================================
  25. *
  26. * File Name:  ncbimisc.c
  27. *
  28. * Author:  Gish, Kans, Ostell, Schuler
  29. *
  30. * Version Creation Date:   10/23/91
  31. *
  32. * $Revision: 2.3 $
  33. *
  34. * File Description: 
  35. *       miscellaneous functions
  36. *
  37. * Modifications:  
  38. * --------------------------------------------------------------------------
  39. * Date     Name        Description of modification
  40. * -------  ----------  -----------------------------------------------------
  41. * 06-15-93 Schuler     This file created (SGML functions were in ncbimisc)
  42. * 12-22-93 Schuler     Converted ERRPOST((...)) to ErrPostEx(...)
  43. *
  44. * ==========================================================================
  45. */
  46.  
  47. #undef  THIS_MODULE
  48. #define THIS_MODULE g_corelib
  49. #define THI_FILE _this_file
  50.  
  51. #include <ncbi.h>
  52. #include <ncbiwin.h>
  53.  
  54. extern char * g_corelib;
  55. static char * _this_file = __FILE__;
  56.  
  57. /* maximum character from SGML to pass to ErrPost()  */
  58. #define SGML_ERROR_MSG_LIM 100
  59.  
  60. static char **sgml_entity;
  61. static char **sgml_ascii;
  62. static int num_sgml_entity;
  63.  
  64.  
  65. /*****************************************************************************
  66. *
  67. *   SgmlLoadTable()
  68. *
  69. *****************************************************************************/
  70. int LIBCALL  Nlm_SgmlLoadTable (void)
  71. {
  72.     char buf[80], *p1, *p2;
  73.     FILE *fp;
  74.     int i, x;
  75.  
  76.     if (num_sgml_entity > 0)
  77.         return (int)num_sgml_entity;
  78.  
  79.     if (! FindPath("ncbi", "ncbi", "data", buf, sizeof (buf)))
  80.         ErrPostEx(SEV_INFO,E_SGML,1,"SgmlLoadTable:  FindPath(ncbi,ncbi,data,...) failed");
  81.     strncat(buf,"sgmlbb.ent",sizeof buf);
  82.  
  83.     if ((fp = FileOpen(buf,"r")) ==NULL)
  84.         return FALSE;
  85.     
  86.     if (!fgets(buf,sizeof buf,fp))
  87.     {
  88.         ErrPostEx(SEV_WARNING,E_SGML,1,"Error reading file [sgmlbb.ent]");
  89.         return FALSE;
  90.     }
  91.     x = atoi(buf);
  92.     sgml_entity = (char**)MemNew(x * sizeof(char*));
  93.     sgml_ascii = (char**)MemNew(x * sizeof(char*));
  94.     for (i = 0; i < x; i++)
  95.     {
  96.         if ((p1 = fgets(buf,sizeof buf,fp)) == NULL) break;
  97.         while ((*p1 < ' ') && (*p1 != '\0')) p1++;  /* skip any leading junk */
  98.         if (*p1 == '\0') break;
  99.         if ( ! (p2 = strchr(p1,'\t')) )   break;
  100.         *p2++ = '\0';
  101.         sgml_entity[i] = StrSave(p1);
  102.         p1 = p2;
  103.         while (*p2 >= ' ') p2++;
  104.         *p2 = '\0';
  105.         sgml_ascii[i] = StrSave(p1);
  106.     }
  107.     FileClose(fp);
  108.     num_sgml_entity = x;
  109.     return x;
  110. }
  111.  
  112. /*****************************************************************************
  113. *
  114. *   Sgml2Ascii(sgml, ascii, buflen)
  115. *
  116. *****************************************************************************/
  117. char * LIBCALL  Nlm_Sgml2Ascii (const char *sgml, char *ascii, Nlm_sizeT buflen)   /* length of ascii buffer */
  118. {
  119.     const char *from;
  120.     char tbuf[40], *to, *tmp;
  121.     int i, j;
  122.  
  123.     if (! num_sgml_entity)
  124.         SgmlLoadTable();
  125.  
  126.     from = sgml;
  127.     to = ascii;
  128.     *to = '\0';
  129.     buflen--;
  130.     *(to+buflen) = '\0';
  131.  
  132.     while ((*from != '\0') && (buflen))
  133.     {
  134.         if (*from == '&')      /* entity start */
  135.         {
  136.             from++;
  137.             tmp = tbuf;
  138.             j = 0;
  139.             while ((*from != ';') && (*(from + 1) != '\0') && (j < 39))
  140.             {
  141.                 *tmp++ = *from++;
  142.                 j++;
  143.             }
  144.             *tmp = '\0';
  145.             from++;
  146.             for (i = 0; i < num_sgml_entity; i++)
  147.             {
  148.                 if (! Nlm_StringCmp(tbuf, sgml_entity[i]))
  149.                     break;
  150.             }
  151.             if (i >= num_sgml_entity)
  152.             {
  153.                 char bad[SGML_ERROR_MSG_LIM];
  154.                 bad[0] = '\0';
  155.                 strncat(bad,sgml,SGML_ERROR_MSG_LIM);
  156.                 ErrPostEx(SEV_ERROR,E_SGML,3,"Unrecognized SGML entity &%s in [%s]",tbuf,bad);
  157.             }
  158.             else
  159.             {
  160.                 *to = '<'; to++;
  161.                 to = Nlm_StringMove(to, sgml_ascii[i]);
  162.                 *to = '>'; to++;
  163.                 buflen -= (Nlm_StringLen(sgml_ascii[i]) + 2);
  164.             }
  165.         }
  166.         else if (*from == '<')   /* region tag */
  167.         {
  168.             while ((*from != '>') && (*from != '\0'))    /* ignore for now */
  169.                 from++;
  170.             if (*from == '\0')
  171.             {
  172.                 char bad[SGML_ERROR_MSG_LIM];
  173.                 bad[0] = '\0';
  174.                 strncat(bad,sgml,SGML_ERROR_MSG_LIM);
  175.                 ErrPostEx(SEV_ERROR,E_SGML,2, "Unbalanced <> in SGML [%s]",bad);
  176.             }
  177.             else
  178.                 from++;
  179.         }
  180.         else
  181.         {
  182.             *to = *from;
  183.             to++; from++; buflen--;
  184.         }
  185.     }
  186.  
  187.     *to = '\0';
  188.     return to;
  189. }
  190.  
  191. /*****************************************************************************
  192. *
  193. *   Sgml2AsciiLen(sgml)
  194. *
  195. *****************************************************************************/
  196. Nlm_sizeT LIBCALL  Nlm_Sgml2AsciiLen (const char *sgml)
  197. {
  198.     const char *from;
  199.     char tbuf[40], *tmp;
  200.     int len = 0, i, j;
  201.  
  202.     if (! num_sgml_entity)
  203.         SgmlLoadTable();
  204.  
  205.     from = sgml;
  206.  
  207.     while (*from != '\0')
  208.     {
  209.         if (*from == '&')      /* entity start */
  210.         {
  211.             from++;
  212.             tmp = tbuf;
  213.             j = 0;
  214.             while ((*from != ';') && (*(from + 1) != '\0') && (j < 39))
  215.             {
  216.                 *tmp++ = *from++;
  217.                 j++;
  218.             }
  219.             *tmp = '\0';
  220.             from++;
  221.             for (i = 0; i < num_sgml_entity; i++)
  222.             {
  223.                 if (! Nlm_StringCmp(tbuf, sgml_entity[i]))
  224.                     break;
  225.             }
  226.             if (i >= num_sgml_entity)
  227.             {
  228.                 char bad[SGML_ERROR_MSG_LIM];
  229.                 bad[0] = '\0';
  230.                 strncat(bad,sgml,SGML_ERROR_MSG_LIM);
  231.                 ErrPostEx(SEV_ERROR,E_SGML,3,"Unrecognized SGML entity &%s in [%s]",tbuf,bad);
  232.             }
  233.             else
  234.                 len += ((Nlm_Int2)Nlm_StringLen(sgml_ascii[i]) + 2);
  235.         }
  236.         else if (*from == '<')   /* region tag */
  237.         {
  238.             while ((*from != '>') && (*from != '\0'))    /* ignore for now */
  239.                 from++;
  240.             if (*from == '\0')
  241.             {
  242.                 char bad[SGML_ERROR_MSG_LIM];
  243.                 bad[0] = '\0';
  244.                 strncat(bad,sgml,SGML_ERROR_MSG_LIM);
  245.                 ErrPostEx(SEV_ERROR,E_SGML,2,"Unbalanced <> in SGML [%s]",bad);
  246.             }
  247.             else
  248.                 from++;
  249.         }
  250.         else
  251.         {
  252.             from++; len++;
  253.         }
  254.     }
  255.  
  256.     return len;
  257. }
  258.  
  259.